package com.ylbh.modules.accessory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ylbh.common.utils.FastJsonUtil;
import com.ylbh.common.utils.HttpClientHelperUtils;
import com.ylbh.common.utils.HttpUtils;
import com.ylbh.modules.accessory.entity.HNGoodsPrice;
import org.apache.commons.lang.StringEscapeUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * @author: czx
 * @date: 2019/11/07 17:44
 */
public class Test {

    private static final String CLIENTID = "14485";

    private static final String CLIENTTOKEN = "F56msDSaaOsEvp57M2ooubckB1gPnIhN";

//    private static final String CLIENTID = "5483";
//
//    private static final String CLIENTTOKEN = "e93c665855f5af26aecc50b243a8c29c";

    public static void main(String[] args) throws UnsupportedEncodingException {

        HashMap<String, String> m = new HashMap<>();
        m.put("clientId", CLIENTID);
        m.put("clientToken", CLIENTTOKEN);
        m.put("goodsSku", "HN1075507141");
        m.put("page","1");
//        m.put("categoryId", "244");
        m.put("method", "goods.getGoodsDetailBySku");
        String express = HttpUtils.sendPost("http://www.houniao.hk/api/route", m);
        com.alibaba.fastjson.JSONObject hnprice = JSON.parseObject(express);
        String dataPrice = hnprice.getString("data");
        List<HNGoodsPrice> hnGoodsPrices = FastJsonUtil.toList(dataPrice, HNGoodsPrice.class);
        dataPrice = dataPrice.substring(0, dataPrice.length() - 1);
        dataPrice = dataPrice.substring(1, dataPrice.length());
        HNGoodsPrice hnGoodsPrice = FastJsonUtil.toBean(dataPrice, HNGoodsPrice.class);
//        String data = jsonObject1.getString("express");
//        System.out.println(StringEscapeUtils.unescapeJava(data));
//        data = data.substring(0, data.length() - 1);
//        data = data.substring(1, data.length());
//        HNGoodsPrice hnGoodsPrice = FastJsonUtil.toBean(data, HNGoodsPrice.class);
        System.out.println();
        // 解码
        String keyWord = URLDecoder.decode("%C4%E3%BA%C3", "UTF-8");
        System.out.println(keyWord);
        // 编码
        String urlEnCode = "";
        System.out.println(urlEnCode);
//        m.put("requestData", urlEnCode);
//        m.put("requestType", "goods.stock");
//        m.put("dataSign","");
        String s = MD5(urlEnCode + CLIENTTOKEN);
        System.out.println(s);
        String getCategory = HttpUtils.sendPost("http://www.houniao.hk/api/route", m);
        System.out.println(new org.json.JSONObject(getCategory));
    }

    public final static String MD5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
